from bs4 import BeautifulSoup
import csv
from selenium import webdriver
import pandas as pd

popular_city = ['beijing1', 'shanghai2', 'chengdu104', 'guangzhou152', 'hangzhou14',
                'chongqing158', 'shenzhen26', 'suzhou11', 'nanjing9', 'xian7',
                'xiamen21', 'tianjin154', 'wuhan145', 'jiaxing272', 'sanya61',
                'kunming29', 'changsha148', 'wuxi10', 'jilin267', 'foshan207',
                'lijiang32', 'dali1445616', 'qingdao5', 'zhengzhou157']
city_name = ['北京', '上海', '成都', '广州', '杭州',
             '重庆', '深圳', '苏州', '南京', '西安',
             '厦门', '天津', '武汉', '嘉兴', '三亚',
             '昆明', '长沙', '无锡', '吉林', '佛山',
             '丽江', '大理', '青岛', '郑州']
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 '
                  'Safari/537.36 Edg/109.0.1518.70',
    'cookie': 'SearchHistoryWord=%u82CF%u5DDE%23--%u6C5F%u82CF%23%3D%3Dhttp%3A//you.ctrip.com/place/11.html%23%26%26'
              '%u4E5F%u7136%B7%u9047%u89C1%u9152%u5E97%uFF08%u5357%u4EAC%u592B%u5B50%u5E99%u8001%u95E8%u4E1C%u5E97'
              '%uFF09%23--%u5357%u4EAC%23%3D%3Dhttp%3A//hotels.ctrip.com/hotel/19110421.html%3Fcityid%3D12%23%26%26'
              '%u897F%u5B89%23--%u9655%u897F%23%3D%3Dhttp%3A//you.ctrip.com/place/7.html%23%26%26%u53A6%u95E8%23'
              '--%u798F%u5EFA%23%3D%3Dhttp%3A//you.ctrip.com/place/21.html%23%26%26%u9E21%u6797%23--%u5E86%u5DDE%23'
              '%3D%3Dhttp%3A//you.ctrip.com/sight/jilin431/133594.html; '
              'ASP.NET_SessionSvc=MTAuMTEzLjkyLjkwfDkwOTB8b3V5YW5nfGRlZmF1bHR8MTYzODQzNDA2MTc3Nw; '
              'Hm_lvt_e4211314613fcf074540918eb10eeecb=1674966577; MKT_CKID=1674966579193.4ocq8.1p64; '
              'MKT_CKID_LMT=1674966579193; _RF1=36.249.106.250; _RSG=nsKjzK9PjR0UYHztJ1Zjk9; '
              '_RDG=2869add140c97027940bd16ca3ee12d6b9; _RGUID=06ed6495-3db5-4db1-9bad-8c14f7f5189e; '
              '_bfaStatusPVSend=1; GUID=09031141116020295512; nfes_isSupportWebP=1; nfes_isSupportWebP=1; '
              'ibulanguage=CN; ibulocale=zh_cn; cookiePricesDisplayed=CNY; '
              '_pd=%7B%22r%22%3A1%2C%22_d%22%3A271%2C%22_p%22%3A2%2C%22_o%22%3A60%2C%22s%22%3A336%2C%22_s%22%3A1%7D; '
              'login_uid=451101F35F63350BC3722E892459D45F; login_type=0; '
              'AHeadUserInfo=VipGrade=5&VipGradeName=%B0%D7%D2%F8%B9%F3%B1%F6&UserName=&NoReadMessageCount=0; '
              'DUID=u=451101F35F63350BC3722E892459D45F&v=0; IsNonUser=F; '
              'cticket=64851ABED6D93301252624A30BD35A2EA38407C1C82E77DEA6730DFCDD24DDE8; '
              'UUID=A20E654E8B654372BAD0861BD905E2E5; IsPersonalizedLogin=F; '
              'Hm_lpvt_e4211314613fcf074540918eb10eeecb=1674972169; '
              '_bfa=1.1674966578822.32ycxm.1.1674966578822.1674966578822.1.72.1; _bfs=1.72; '
              '_ubtstatus=%7B%22vid%22%3A%221674966578822.32ycxm%22%2C%22sid%22%3A1%2C%22pvid%22%3A72%2C%22pid%22'
              '%3A290570%7D; _jzqco=%7C%7C%7C%7C1674966580401%7C1.1094061991.1674966579209.1674972161922'
              '.1674972171821.1674972161922.1674972171821.undefined.0.0.66.66; '
              '__zpspc=9.1.1674966579.1674972171.66%234%7C%7C%7C%7C%7C%23; '
              '_bfi=p1%3D290570%26p2%3D290570%26v1%3D72%26v2%3D71; _bfaStatus=success',
    'referer': 'https://passport.ctrip.com/'

}
want_to = []
have_gone = []
data = []


def tourist_intention():
    cnt = 0
    reflect = 0
    bro = webdriver.Chrome(executable_path=r'C:\Users\86159\AppData\Local\Google\Chrome\Application\chromedriver.exe')
    for city in popular_city:
        print(city)
        url = 'https://you.ctrip.com/travels/' + city + '.html'
        # page_text = requests.get(url, headers=headers).content

        bro.get(url)
        # 获取浏览器当前页面的源码数据
        page_text = bro.page_source
        soup = BeautifulSoup(page_text, "lxml")
        will = soup.select("#emWantValueID")[0].string
        went = soup.select("#emWentValueID")[0].string
        want_to.append(will)
        have_gone.append(went)
        name = city_name[reflect]
        reflect += 1
        data.append((name, will, went))
        # if cnt < 1:
        #     cnt += 1
        # else:
        #     break
    print(data)
    with open("../csv文件/携程全国城市热度.csv", "w+", newline="") as f:
        header = ['城市', '想去的人', '去过的人']
        writer = csv.writer(f)
        writer.writerow(header)
        for i in data:
            i = str(i)
            tmp = i.replace("(", "").replace(")", "").replace("'", "").replace(" ", "").split(",")
            writer.writerow(tmp)


if __name__ == '__main__':
    tourist_intention()
